package com.twoGroup.boot.controller;

import com.twoGroup.boot.pojo.CartAndShopAndUserAndGoods;
import com.twoGroup.boot.pojo.Page;
import com.twoGroup.boot.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/cart")
@Api(tags = "购物车接口")
public class CartController {
    public static Logger logger = LoggerFactory.getLogger(CartController.class);
    @Resource
    private CartService cartService;

    @ApiOperation(value = "添加商品到购物车", notes = "输入用户id,商品id,购买数量")
    @PostMapping("/addGoods")
    @ResponseBody
    //添加商品到购物车中
    public int addGoods(Integer userId, Integer goodsId, Integer shopId, Integer goodsCount){
       return cartService.addGoods(userId, goodsId, shopId, goodsCount);
    }

    @ApiOperation(value = "删除购物车中某个商品", notes = "输入用户id,商品id")
    @PostMapping("/deleteGoodsCart")
    @ResponseBody
    //删除购物车中的某个商品
    public int deleteGoodsCart(Integer userId, Integer goodsId){
        logger.info("删除成功----------------------");
        return cartService.deleteGoodsCart(userId, goodsId);
    }

    @ApiOperation(value = "清空购物车", notes = "输入用户id")
    @PostMapping("/deleteAllGoodsCart")
    @ResponseBody
    //清空购物车
    public int deleteAllGoodsCart(Integer userId){
        logger.info("清空成功--------------------");
        return cartService.deleteAllGoodsCart(userId);
    }

    /**
     * 变更商品数量
     * @param userId 用户id
     * @param goodsId 商品id
     * @param goodsCount 变更数量  变更之后的数量也要小于商品的库存
     * @return 1
     */
    public int updateGoodsCart(Integer userId, Integer goodsId, Integer goodsCount){
        return cartService.updateGoodsCart(userId, goodsId, goodsCount);
    }

    //根据id查询用户的购物车
    @ApiOperation(value = "购物车查询", notes = "输入用户id")
    @PostMapping("/queryUserCart")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId",value = "用户id"),
                    @ApiImplicitParam(name="pageNow",value = "当前页面"),
                    @ApiImplicitParam(name = "pageSize",value = "当前页面显示数量"),
            }
    )
    @ResponseBody
    public Map<String, Object> queryUserCart(Integer userId, int pageNow, int pageSize, HttpServletRequest request){
        Page<CartAndShopAndUserAndGoods> userCart = cartService.queryUserCart(userId, pageNow, pageSize);
        for (CartAndShopAndUserAndGoods a : userCart.getList()) {
            logger.info(String.valueOf(a));
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userCart",userCart);
        return map;
    }



}
